// 获取储存的常量
import { LOGIN } from './mutation-types'
// 获取接口
import { authLogin } from '~/api/login.js'

import { getCookieValue } from '~/util/index.js';
import aes from '~/util/aes.js'

export default {

  //nuxt扩展的方法(在服务器端初始化时执行)
  nuxtServerInit({
    commit
  }, {
    req
  }) {
    let cookie = req.headers.cookie;
    let token = getCookieValue('token', cookie);
    commit('setToken', token);
    let user = getCookieValue('user', cookie)
    commit('setUser', user ? JSON.parse(aes.Decrypt(user)) : {})
    // let agent = getCookieValue('agent', cookie)
    // commit('setAgent', agent ? JSON.parse(aes.Decrypt(agent)) : {})
  },

  async authLogin(context) {
    let params = {
      method: 1,
      sourse: 'admin',
      phone: '18866668888',
      password: '123456'
    }
    let { data } = await authLogin(params)
    context.commit(LOGIN, data.user)
  },

}

